#include "Console.h"
#include <K_Windows.h>
#include <ShellAPI.h>
#include <KGlobal.h>
#include "..\resource.h"

K_ANONYMOUS_NAMESPACE_BEG

HICON hLarge[Console::Count];
HICON hSmall[Console::Count];

K_ANONYMOUS_NAMESPACE_END

void Console::freeIcon()
{
    for (size_t i = 0; i < Console::Count; ++i) {
        if (hLarge[i]) DestroyIcon(hLarge[i]);
        if (hSmall[i]) DestroyIcon(hSmall[i]);
    }
}

void Console::loadIcon(const char *filename)
{
    assert(filename != NULL);
    ExtractIconExA(filename, -IDI_ICON1, hLarge + 0, hSmall + 0, 1);
    ExtractIconExA(filename, -IDI_ICON2, hLarge + 1, hSmall + 1, 1);
    ExtractIconExA(filename, -IDI_ICON3, hLarge + 2, hSmall + 2, 1);
}

void Console::setIcon(State state)
{
    HWND hConsole = GetConsoleWindow();
    if (IsWindow(hConsole)) {
        if (hLarge[state]) SendMessage(hConsole, WM_SETICON, ICON_BIG, reinterpret_cast<LPARAM>(hLarge[state]));
        if (hSmall[state]) SendMessage(hConsole, WM_SETICON, ICON_SMALL, reinterpret_cast<LPARAM>(hSmall[state]));
    }
}